﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataCentral.UsecaseControllers;
using CommonData.CommonItems;
using CommonData;

namespace DataCentral.Forms
{
    public partial class EditSalePersonForm : CommonData.CommonChildForm
    {
        public SalePersonInfo IFSalePerson
        {
            get;
            set;
        }

        public EditSalePersonForm(SalePersonInfo ifSalePerson)
        {
            InitializeComponent();
            IFSalePerson = ifSalePerson;
            rdgSaleType.SelectedIndex = ifSalePerson.SalePerTye.Equals("NVBH") ? 0 : 1;
            txtSaleCode.Text = ifSalePerson.SalePersonCode;
            txtSaleName.Text = ifSalePerson.SalePersonName;
        }

        private bool CheckValidData()
        {
            bool isValid = true;

            if (string.IsNullOrEmpty(txtSaleName.Text))
            {
                CommonFunctions.ShowErrorProvider(errProvider, txtSaleName, "Tên nhân viên bán hàng không được để trống.");
                isValid = false;
            }
            if (string.IsNullOrEmpty(txtSaleCode.Text))
            {
                CommonFunctions.ShowErrorProvider(errProvider, txtSaleCode, "Mã nhân viên bán hàng không được để trống.");
                isValid = false;
            }

            if (rdgSaleType.SelectedIndex < 0)
            {
                CommonFunctions.ShowErrorProvider(errProvider, rdgSaleType, "Hãy chọn nhân viên là bán hàng hay giao hàng.");
                isValid = false;
            }

            return isValid;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckValidData())
            {
                IFSalePerson.SalePerTye = rdgSaleType.SelectedIndex == 0 ? "NVBH" : "NVGH";
                IFSalePerson.SalePersonCode = txtSaleCode.Text.Trim();
                IFSalePerson.SalePersonName = txtSaleName.Text.Trim();

                CommonDataAccess cmDataAccess = new CommonDataAccess();
                List<ParameterObject> lstParams = new List<ParameterObject>();
                lstParams.Add(new ParameterObject("@SalePersonID", IFSalePerson.SalePersonID));
                lstParams.Add(new ParameterObject("@SlsperType", IFSalePerson.SalePerTye));
                lstParams.Add(new ParameterObject("@SlsperCode", IFSalePerson.SalePersonCode));
                lstParams.Add(new ParameterObject("@Name", IFSalePerson.SalePersonName));
                lstParams.Add(new ParameterObject("@SlsTeamID", 0));
                cmDataAccess.ExecuteSprocNonQuery("DATA_InsertUpdateSalePerson", lstParams);

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageClass.ShowErrorMessage(ConstantClass._Error_InvalidData);
            }
        }
    }
}
